package com.haofengsoft.scce;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.haofengsoft.platform.utils.PhoneUtil;
import com.haofengsoft.platform.utils.StringUtil;
import com.haofengsoft.scce.common.client.VolleyBean;
import com.haofengsoft.scce.common.client.VolleyInterface;
import com.haofengsoft.scce.common.client.VolleyManage;
import com.haofengsoft.scce.common.config.ConstantConfig;

/**
 * 注册
 * 
 * @author zhj
 * 
 */
public class RegistActivity extends Activity {

	private Button nextBtn;// 下一步
	private EditText telEt;// 手机号
	private Button vertifyCodeBtn;// 获取验证码
	private EditText vertifyCodeEt;// 填写验证码
	/**
	 * 计时器
	 */
	private int time = 120;
	private TimerTask task;
	private Timer timer = new Timer();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		AppManage.getInstance().addActivity(RegistActivity.this);
		setContentView(R.layout.regist);
		telEt = (EditText) findViewById(R.id.register_telephone_et);
		nextBtn = (Button) findViewById(R.id.register_next_step_btn);
		ImageView backIv = (ImageView) findViewById(R.id.register_back_iv);
		vertifyCodeBtn = (Button) findViewById(R.id.register_identify_btn);
		vertifyCodeEt = (EditText) findViewById(R.id.register_vertify_code_et);
		nextBtn.setOnClickListener(new NextstepClickListener());
		backIv.setOnClickListener(new BackClickListener());
		vertifyCodeBtn.setOnClickListener(new VertifyCodeClickListener());

	}

	/**
	 * 验证码计时器
	 */
	public void verificationCount() {
		task = new TimerTask() {
			@SuppressLint("ResourceAsColor")
			@Override
			public void run() {

				runOnUiThread(new Runnable() {
					@SuppressLint("ResourceAsColor")
					@Override
					public void run() {
						if (time <= 0) {
							buttonEnabledTrue();
						} else {
							buttonEnabledFalse();
						}
						time--;
					}
				});
			}
		};
		time = 60;
		timer.schedule(task, 0, 1000);
	}

	/**
	 * 禁止点击
	 */
	public void buttonEnabledFalse() {
		vertifyCodeBtn.setText(time + "S后重新获取");
		vertifyCodeBtn.setEnabled(false);
		vertifyCodeBtn.setBackgroundColor(getResources().getColor(R.color.light_grey));
		vertifyCodeBtn.setTextColor(getResources().getColor(R.color.silver));
	}

	/**
	 * 允许点击
	 */
	public void buttonEnabledTrue() {
		task.cancel();
		vertifyCodeBtn.setText("获取验证码");
		vertifyCodeBtn.setBackgroundColor(getResources().getColor(R.color.common_btn_nor));
		vertifyCodeBtn.setEnabled(true);
		vertifyCodeBtn.setTextColor(getResources().getColor(R.color.white));
	}

	/**
	 * 下一步
	 * 
	 * @author zhj
	 * 
	 */
	class NextstepClickListener implements OnClickListener {

		@Override
		public void onClick(View view) {

			String telephoneNumer = telEt.getText().toString();
			if (StringUtil.isEmpty(telephoneNumer)) {
				Toast.makeText(RegistActivity.this, "请输入手机号", Toast.LENGTH_SHORT).show();
				return;
			}
			String vertifyCode = vertifyCodeEt.getText().toString();
			if (StringUtil.isEmpty(vertifyCode)) {
				Toast.makeText(RegistActivity.this, "请输入验证码", Toast.LENGTH_SHORT).show();
				return;
			}
			Map<String, Object> mapParam = new HashMap<String, Object>();
			mapParam.put("Mobile", telephoneNumer);
			mapParam.put("VerificationCode", vertifyCode);
			VolleyManage.getInstance().methodPost(RegistActivity.this, "处理中...",
					ConstantConfig.REGIST_VERTIFY_CODE_VALID_URL, mapParam, new VertifyInfoVolleyReal());
		}
		// 注册通过，设置密码

	}

	class VertifyInfoVolleyReal implements VolleyInterface {

		@Override
		public void gainData(VolleyBean bean) {
			if (bean.isSuccess()) {
				Intent intent = new Intent(RegistActivity.this, SetPassWordActivity.class);
				intent.putExtra("account", telEt.getText().toString());
				startActivity(intent);
				finish();
			} else {
				Toast.makeText(RegistActivity.this, "验证码错误", Toast.LENGTH_SHORT).show();
			}
		}
	}

	class BackClickListener implements OnClickListener {

		@Override
		public void onClick(View view) {
			finish();

		}

	}

	/**
	 * 获取验证码
	 * 
	 * @author zhj
	 * 
	 */
	class VertifyCodeClickListener implements OnClickListener {

		@Override
		public void onClick(View view) {
			String telephoneNumer = telEt.getText().toString();
			if (StringUtil.isEmpty(telephoneNumer)) {
				Toast.makeText(RegistActivity.this, "请输入手机号码", Toast.LENGTH_SHORT).show();
				return;
			}
			if (!PhoneUtil.isMobileNO(telephoneNumer.trim())) {
				Toast.makeText(RegistActivity.this, "请您输入有效的手机号码", Toast.LENGTH_SHORT).show();
				return;
			}
			vertifyCodeBtn.setEnabled(false);
			Map<String, Object> mapParam = new HashMap<String, Object>();
			mapParam.put("Mobile", telephoneNumer);
			mapParam.put("Type", "注册");
			VolleyManage.getInstance().methodPost(RegistActivity.this, null, ConstantConfig.REGIST_VERTIFY_CODE_URL,
					mapParam, new VertifyCodeReal());

		}
	}

	class VertifyCodeReal implements VolleyInterface {

		@Override
		public void gainData(VolleyBean bean) {
			vertifyCodeBtn.setEnabled(true);
			if (bean.isSuccess()) {
				// 成功，启动 记时
				verificationCount();
				Toast.makeText(RegistActivity.this, "验证码已发送，请注意查收", Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(RegistActivity.this, bean.getMessage(), Toast.LENGTH_SHORT).show();
			}
		}
	}
}
